<?php

namespace Admin\Controller;


class AdvertController extends CommonController
{
    /**
     * 广告首页
     */
    public function index()
    {
        //分页
        $count = $this->advert->count();// 查询满足要求的总记录数
        $Page = new \Common\Util\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $adverts = $this->advert->order("sort_order desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('adverts', $adverts);
        $this->assign('page', $show);
        $this->assign('count', $count);
        $this->display();
    }

    /***
     * 广告首页--新增
     */
    public function add()
    {
        if (IS_POST) {
            $data['title'] = trim(I('post.title'));
            $data['thumb'] = I('post.thumb');
            $data['url'] = I('post.url');
            $data['sort_order'] = I('post.sort_order');
            $data['description'] = trim(I('description'));

            if (strlen($data['title']) < 2) {
                $this->error('广告名的长度不能小于2位');
            }
            if (strlen($data['url']) == "") {
                $this->error('url地址不能为空');
            }
            if (strlen($data['description']) == "") {
                $this->error('广告信息不能为空');
            }

            $this->advert->create();
            $this->advert->add($data);
            $this->success('新增广告成功', U('index'));
        } else {
            $this->display();

        }
    }

    /**
     * 广告首页---编辑
     */
    public function edit()
    {
        $id = I('get.id');
        if (IS_POST) {
            $data['title'] = trim(I('post.title'));
            $data['thumb'] = I('post.thumb');
            $data['url'] = I('post.url');
            $data['sort_order'] = I('post.sort_order');
            $data['description'] = trim(I('description'));

            if (strlen($data['title']) < 2) {
                $this->error('广告名的长度不能小于2位');
            }
            if (strlen($data['url']) == "") {
                $this->error('url地址不能为空');
            }
            if (strlen($data['description']) == "") {
                $this->error('广告信息不能为空');
            }

            $this->advert->create();
            $this->advert->where("id = '$id'")->save($data);
            $this->success('编辑广告成功', U('index'));
        } else {
            $this->adverts = $this->advert->where("id = '$id'")->find();
            $this->display();
        }

    }

    /**
     * 广告首页---ajax删除
     */
    public function ajax_delete(){
        if(IS_AJAX){
            $id = I('post.id');
            $this->advert->where("id = '$id'")->delete();
        }
    }

    /***
     * 广告首页---ajax多选删除
     */
    public function ajax_delete_all(){
        if(IS_AJAX){
            $ids = I("post.ids");
            foreach ($ids as $id){
                $this->advert->where("id = '$id'")->delete();
            }
        }
    }

    /**
     * 广告首页---ajax排序
     */
    public function ajax_sort_order(){
        if(IS_AJAX){
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->advert->where("id = '$id'")->setField('sort_order',$sort_order);
        }
    }

}